/*
 * DA1.asm
 *
 *  Created: 2/23/2015 8:45:46 PM
 *   Author: Clinton
 */ 

 .equ RAMSTART = 0x0100
 .def zero = r0
 .def one = r1
 .def three = r5
 .def rem = r2
 .def storeCount = r10
 .def threeCount = r12
 .def sevenCount = r13
 .def seven = r14
 

 SUB zero, zero		;clear R0
 SUB sevenCount, sevenCount		;clear seven counter
 SUB threeCount, threeCount		;clear three counter
 SUB R20, R20					;clear r20 register
 SUB R21, R21					;clear r21 register
 SUB R23, R23					;clear r23 register
 SUB R24, R24					;clear r24 register

 LDI R16, 1				
 MOV one,R16			;store 1 in one register
 LDI R16, 3
 MOV three, R16			;store 3 in three register
 LDI R16, 7
 MOV seven, R16			;store 7 in seven


 
 LDI R16, 25
 MOV storeCount, R16			;store the number of values to write
 LDI R16, HIGH(RAMSTART)
 LDI R17, LOW(RAMSTART)

 LDI R18, HIGH(RAMEND) ; SETTING UP STACK
 LDI R19, LOW(RAMEND)

 ADD R19, R17		;add lows
 ADC R18, R16		;add highs + carry
  
 MOV XL, R19		;store part of address in x
 MOV XH, R18		;store high part of address in x

 MOV YL, XL			;move low part of x into low part of y

 LSR YL				;divide y by 2
 LSR YH

 MOV rem, YL			;store YL in remainder register
	
 
 Get3Factor:
 
 SUB rem, three			;start division process
 CP rem, one			;compare if REM is less than 1
 BRLT LessThan1		;branches if less than 1
 jmp Get3Factor
 
 LessThan1:

 CP rem, zero				;checks to see if number is divisible by 3
 BRLT NegativeRem			;branches to NegativeRem if value is negative
 MOV threeCount, three		;set counter to 3
 jmp G3FDone				;jumps to G3FDone

 NegativeRem:

 add rem, three				;provides positive counter value
 mov threeCount, rem		;save counter

 G3FDone:

 MOV REM, YL				;loads value at first address

 Get7Factor:
 
 SUB rem, seven				;start division process
 CP rem, one				;compare if REM is less than 1
 BRLT LessThan1seven		;branches if less than 1
 jmp Get7Factor
 
 LessThan1seven:

 CP rem, zero				;checks to see if value is divisible by 7
 BRLT NegativeRemSeven		;branches if value is negative
 MOV sevenCount, seven		;set counter to 7
 jmp G7FDone				;jumps to G7FDone
 
 NegativeRemSeven:

 add rem, seven				;gives counter a positive value
 mov sevenCount, rem		;saves counter
 
 G7FDone:					
 
 StoreNums:

 ST X+, YL					;stores data in address and post-increments
 CP  threeCount, three		;checks to see if value is divisible by 3
 BREQ D3Sum					;branches if value is divisible by 3


 BeforeSeven:

 CP sevenCount, seven		;checks to see if value is divisible by 7
 BREQ D7Sum					;branches is value is divisible by 7

 nextAdd:
  

 INC threeCount				;increments three counter
 INC sevenCount				;increments seven counter

 INC YL						;increments the value obtained from first address
 DEC storeCount				;decrements the counter for stored values
 BREQ Done					;program is complete
 jmp StoreNums				;jumps to store the next number

 D3Sum:	 

 mov threeCount, zero		;sets three counter to 0
 add R23, YL				;adds value that was divisible by three to a register that holds the sum
 adc R24, zero				;adds the carry that may have been obtained in previous operation
 jmp BeforeSeven			;jumps back into the routine that stores numbers

 D7Sum:	 

 mov sevenCount, zero		;sets seven counter to 0
 add R20, YL				;adds value that was divisible by seven to a register that holds the sum
 adc R21, zero				;adds the carry that may have been obtained in previous operation
 jmp nextAdd				;jumps back into the routine that stores numbers
 

 Done:						;program is complete
 jmp Done

 ;MOV storeCount, four

 CheckStorage:


; LD R1, -X
 ;DEC R20
 ;BREQ Exit
 ;jmp CheckStorage

 Exit:





 





 

